<template>
  <a-row :gutter="10">
    <a-col :md="24" :sm="24">
      <a-card :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <a-col :md="4" :sm="6">
                <a-form-item label="所属项目">
                  <a-select v-model="queryParam.projectCode"  v-decorator="[ 'projectCode']" placeholder="请选择所属项目">
                    <a-select-option v-for="item in projectList" :key="item.id" :value="item.code">
                      {{item.name}}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- 查询区域-END -->

        <!-- 操作按钮区域 -->
        <div class="table-operator">
          <a-button @click="handleAddLight" type="primary" icon="plus">新增</a-button>
          <a-button @click="readLightData" type="primary" icon="plus">读取路灯读取</a-button>
          <a-button type="primary"  icon="download" @click="ledOpenOrClose('确定开灯？',100)">
            开灯
          </a-button>
          <a-button type="primary"  icon="download" @click="ledOpenOrClose('确定关灯？',0)">关闭
          </a-button>
          <a-button type="primary" icon="download" @click="uploadFile('import_light.xls')">模板下载</a-button>

          <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
            <a-button type="primary" icon="import">导入</a-button>
          </a-upload>

          <a-button type="primary"  icon="download" @click="setParameter()">参数设置</a-button>

          <a-dropdown v-if="selectedRowKeys.length > 0">
            <a-menu slot="overlay">
              <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
            </a-menu>
            <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
          </a-dropdown>
        </div>

        <!-- table区域-begin -->
        <div>
          <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
            <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
            <a style="margin-left: 24px" @click="onClearSelected">清空</a>
          </div>

          <a-table
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            :rowSelection="{fixed:true,selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
            @change="handleTableChange">

            <template slot="avatarslot" slot-scope="text, record, index">
              <div class="anty-img-wrap">
                <img :src="getAvatarView(record.currentStatus)"/>
              </div>
            </template>

            <template slot="htmlSlot" slot-scope="text">
              <div v-html="text"></div>
            </template>
            <template slot="imgSlot" slot-scope="text">
              <span v-if="!text" style="font-size: 12px;font-style: italic;">无此图片</span>
              <img v-else :src="getImgView(text)" height="25px" alt="图片不存在" style="max-width:80px;font-size: 12px;font-style: italic;"/>
            </template>
            <template slot="fileSlot" slot-scope="text">
              <span v-if="!text" style="font-size: 12px;font-style: italic;">无此文件</span>
              <a-button
                v-else
                :ghost="true"
                type="primary"
                icon="download"
                size="small"
                @click="uploadFile(text)">
                下载
              </a-button>
            </template>

            <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item>
                <a @click="updateLg(record)">修改经纬度</a>
              </a-menu-item>

            </a-menu>
          </a-dropdown>
        </span>

          </a-table>
        </div>

       <glr-alone-light-model  ref="modalForm" @ok="modalFormOk"></glr-alone-light-model>
        <glr-light-set-parameter ref="glrLightSetParameter" @ok="modalFormOk"></glr-light-set-parameter>
      </a-card>
    </a-col>

  </a-row>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { httpAction } from '@/api/manage'
  import GlrAloneLightModel from "./modules/GlrAloneLightModel";
  import GlrLightSetParameter from "./modules/GlrLightSetParameter";
  export default {
    name: "GlrLightList",
    mixins:[JeecgListMixin],
    components: {
        GlrLightSetParameter,
      GlrAloneLightModel
    },
    data () {
      return {
        description: '路灯管理页面',
        dropTrigger: '',
        gateWayTree:[],
        autoExpandParent:true,
        defaultExpandParent:true,
        iExpandedKeys:[],
        projectList:[],
        queryParam:{
            type:2
          },
        gateWayCode:'',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
            {
                title:'编号',
                align:"center",
                dataIndex: 'type'
            },
          {
            title:'编号',
            align:"center",
            dataIndex: 'code'
          },
          {
              title:'网关编号',
              align:"center",
              dataIndex: 'gateWayCode'
          },
          {
            title:'路灯类型',
            align:"center",
            dataIndex: 'typeName'
          },
          {
            title:'当前电压(V)',
            align:"center",
            dataIndex: 'currentVol'
          },
          {
            title:'当前电流(mA)',
            align:"center",
            dataIndex: 'currentCur'
          },
          {
            title:'当前亮度(%)',
            align:"center",
            dataIndex: 'currentBrightness'
          },
          {
            title:'视在功率',
            align:"center",
            dataIndex: 'currentApparentPower'
          },
          {
            title:'当前功率(W)',
            align:"center",
            dataIndex: 'currentPower'
          },
          {
            title:'当前状态',
            align:"center",
            scopedSlots: {customRender: "avatarslot"}
          },
          {
            title:'当前温度(℃)',
            align:"center",
            dataIndex: 'currentTemperature'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/glr/glrLight/list",
          delete: "/glr/glrLight/delete",
          deleteBatch: "/glr/glrLight/deleteBatch",
          exportXlsUrl: "/glr/glrLight/exportXls",
          importExcelUrl: "glr/glrLight/importExcel",
          readLightData: "glr/glrLight/readLightData",
          getProjectList: "/glr/glrProject/getProject",
          aloneLedOpenOrClose:"/glr/glrLight/aloneLedOpenOrClose"

        },
        dictOptions:{
        },
      }
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    created(){
      const that = this;
      httpAction(that.url.getProjectList,{},"post").then((res)=>{
        if(res.success){
          that.projectList=res.result;
        }else{
          that.$message.warning(res.message);
        }
      }).finally(() => {
        that.confirmLoading = false;
      });
     //   this.queryParam.type =2;
     // this.loadData();
    },
    methods: {
      initDictConfig(){
      },
      getAvatarView: function (status) {
        if (status == 'Y') {
          return require('@/assets/img/zaixian.png');
        } else {
          return require('@/assets/img/lixian.png');
        }
      },
      setThisExpandedKeys(node) {
        if (node.children && node.children.length > 0) {
          this.iExpandedKeys.push(node.key)
          for (let a = 0; a < node.children.length; a++) {
            this.setThisExpandedKeys(node.children[a])
          }
        }
      },
      handleAddLight(){
        this.$refs.modalForm.add(this.queryParam.gateWayCode);
        this.$refs.modalForm.title = "新增";
        this.$refs.modalForm.disableSubmit = false;
      },
      readLightData(){
        if(this.gateWayCode==null||this.gateWayCode==''){
          this.$message.warning('请选择相应网关！');
          return;
        }
        var that = this;
        that.loading=true;
        httpAction(that.url.readLightData, {gateWayCode: that.gateWayCode},"post").then((res) => {
          if (res.success) {
            setTimeout(function(){
              that.loading=false;
              that.$message.success(res.message);
              that.loadData();
              that.onClearSelected();
            }, 3000);
          } else {
            that.$message.warning(res.message);
          }
        }).finally(() => {
          setTimeout(function(){
            that.loading=false;
          }, 3000);
        });
      },
      updateLg(){

      },

      setParameter() {
          if (this.selectionRows.length != 1) {
              this.$message.warning('请选择一条记录！');
              return;
          } else {
              var record;
              for (var a = 0; a < this.selectionRows.length; a++) {
                  record = this.selectionRows[0];
              }
              this.$refs.glrLightSetParameter.edit(record);
          }
      },

      ledOpenOrClose: function (msg, dimmingValue) {
        if (this.selectionRows.length <= 0) {
          this.$message.warning('请选择一条记录！');
          return;
        } else {
          var lightCode="";
          for (var a = 0; a < this.selectionRows.length; a++) {
              lightCode += this.selectionRows[a].code+";"+this.selectionRows[a].gateWayCode + ",";
          }
          var that = this;
          this.$confirm({
            title: "确认",
            content: msg,
            onOk: function () {
              that.loading = true;
              httpAction(that.url.aloneLedOpenOrClose, {
                brightValue: dimmingValue,
                  lightCode:lightCode
              }, "post").then((res) => {
                if (res.success) {
                  that.$message.success(res.message);
                  that.loadData();
                  that.onClearSelected();
                } else {
                  that.$message.warning(res.message);
                }
              }).finally(() => {
                that.loading = false;
              });
            }
          });
        }
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>